package com.boco.fznq.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.boco.fznq.AppConstants;
import com.boco.fznq.R;
import com.boco.fznq.login.bean.UserBean;
import com.boco.fznq.main.MainActivity;
import com.boco.fznq.network.HttpListener;
import com.boco.fznq.network.HttpUtils;
import com.boco.fznq.utils.SPKey;
import com.boco.fznq.utils.SPUtils;
import com.culiu.core.activity.BaseCoreActivity;
import com.culiu.core.fonts.CustomButton;
import com.culiu.core.fonts.CustomEditText;
import com.culiu.core.fonts.CustomTextView;
import com.culiu.core.utils.notification.ToastUtils;
import com.culiu.core.widget.EmptyView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.boco.fznq.login.RegisterActivity.FORGET_PAGE;
import static com.boco.fznq.login.RegisterActivity.REGISTER_PAGE;

/**
 * Created by jiangcheng on 2016/12/12.
 */

public class LoginActivity extends BaseCoreActivity {


    @BindView(R.id.login_phone)
    CustomEditText mLoginPhone;
    @BindView(R.id.login_pwd)
    CustomEditText mLoginPwd;
    @BindView(R.id.login_forgot_pwd)
    CustomTextView mLoginForgotPwd;
    @BindView(R.id.login_btn)
    CustomButton mLoginBtn;
    @BindView(R.id.login_goto_register)
    CustomTextView mLoginGotoRegister;
    @BindView(R.id.root_view)
    EmptyView mRootView;

    private String phone;
    private String pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @OnClick({R.id.login_forgot_pwd, R.id.login_btn, R.id.login_goto_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_forgot_pwd:
                goToRegisterPage(FORGET_PAGE);
                break;

            case R.id.login_btn:
                if (checkPhoneAndPwd()) {
                    login();
                }
                break;

            case R.id.login_goto_register:
                goToRegisterPage(REGISTER_PAGE);
                break;
        }
    }

    /**
     * 检查电话和密码
     *
     * @return
     */
    private boolean checkPhoneAndPwd() {
        phone = mLoginPhone.getText().toString();
        pwd = mLoginPwd.getText().toString();

        String phoneTips = VerifyUtil.verifyPhone(phone);
//        String passwordTips = VerifyUtil.verifyPassword(pwd);
        String passwordTips = "";

        if (!TextUtils.isEmpty(phoneTips)) {
            ToastUtils.showShort(this, phoneTips);
        } else if (!TextUtils.isEmpty(passwordTips)) {
            ToastUtils.showShort(this, passwordTips);
        } else {
            return true;
        }
        return false;
    }

    /**
     * 登陆
     */
    private void login() {

        String loginUrl = AppConstants.HOST +  "/api/login";

        HttpUtils.post(loginUrl, CommonParams.getLoginParams(phone, pwd),
                UserBean.class, new HttpListener<UserBean>() {
                    @Override
                    public void onResponse(UserBean response) {
                        if (response != null && response.isSuccess()) {
                            ToastUtils.showShort(LoginActivity.this, "登陆成功");

                            if (response.getData() != null) {
                                handleLoginSuccess(response.getData());
                            }
                        } else {
                            ToastUtils.showShort(LoginActivity.this, response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showShort(LoginActivity.this, "网络错误，请稍后再试!");
                    }
                });

    }

    private void handleLoginSuccess(UserBean.UserInfo data) {
        SPUtils.put(this, SPKey.TOKEN, data.getToken());
        SPUtils.put(this, SPKey.PHONE, data.getPhoneNum());
        SPUtils.put(this, SPKey.HEADERIMG, data.getAvatarUrl());
        EventBus.getDefault().post(new LoginEvent(data));

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

    }


    private void goToRegisterPage(int pageType) {
        Intent intent = new Intent(this, RegisterActivity.class);
        intent.putExtra("pageType", pageType);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
